package com.stone;

public class SpiralMatrix {

    public static void main(String[] args) {

        a(20);
    }



    public static void a(int n){
        int[ ][ ]  arr =new int[n][n];
        int sum = n*n;
        int  j = 0;

        int top=0;
        int right=n;
        int buttom=n;
        int left=0;

        while (true){
            for (int i = top; i < right; i++) {
                arr[top][i] = j++;
            }
            top++;

            if (top > right){
                break;
            }
            right--;
            for (int i = top; i < buttom; i++) {
                arr[i][right] = j++;
            }
            if (top > buttom){
                break;
            }

            buttom--;
            for (int i = right-1; i >= left; i--) {
                arr[right][i] = j++;
            }

            for (int i = buttom-1; i >= top; i--) {
                arr[i][left]=j++;
            }

            left++;
            System.out.println(top+" "+right+" "+buttom+" "+left+" "+j);
            if (j > sum){
                break;
            }
        }

        for (int i = 0; i < n; i++) {
            System.out.println();
            for (int k = 0; k < n; k++) {
                System.out.print(arr[i][k]+"  ");
            }
        }
    }
}
